﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using HYSoft.IDAL;
using HYSoft.DBUtility;//Please add references
namespace HYSoft.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:bpcddata
	/// </summary>
	public partial class bpcddata:Ibpcddata
	{
		public bpcddata()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ljbp_sec_id", "tb_bpcddata"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(DateTime cldate,int ljbp_sec_id,int cdbh,int cdcgqbh)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from tb_bpcddata");
			strSql.Append(" where cldate=@cldate and ljbp_sec_id=@ljbp_sec_id and cdbh=@cdbh and cdcgqbh=@cdcgqbh ");
			SqlParameter[] parameters = {
					new SqlParameter("@cldate", SqlDbType.DateTime),
					new SqlParameter("@ljbp_sec_id", SqlDbType.Int,4),
					new SqlParameter("@cdbh", SqlDbType.Int,4),
					new SqlParameter("@cdcgqbh", SqlDbType.Int,4)			};
			parameters[0].Value = cldate;
			parameters[1].Value = ljbp_sec_id;
			parameters[2].Value = cdbh;
			parameters[3].Value = cdcgqbh;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(HYSoft.Model.bpcddata model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into tb_bpcddata(");
			strSql.Append("cldate,numofday,ljbp_sec_id,cdbh,cdcgqbh,ljbpwy,ljbpqcqxj_qx,ljbpqcqxj_zx,ljbpjyl)");
			strSql.Append(" values (");
			strSql.Append("@cldate,@numofday,@ljbp_sec_id,@cdbh,@cdcgqbh,@ljbpwy,@ljbpqcqxj_qx,@ljbpqcqxj_zx,@ljbpjyl)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@cldate", SqlDbType.DateTime),
					new SqlParameter("@numofday", SqlDbType.Int,4),
					new SqlParameter("@ljbp_sec_id", SqlDbType.Int,4),
					new SqlParameter("@cdbh", SqlDbType.Int,4),
					new SqlParameter("@cdcgqbh", SqlDbType.Int,4),
					new SqlParameter("@ljbpwy", SqlDbType.Int,4),
					new SqlParameter("@ljbpqcqxj_qx", SqlDbType.Int,4),
					new SqlParameter("@ljbpqcqxj_zx", SqlDbType.Int,4),
					new SqlParameter("@ljbpjyl", SqlDbType.Int,4)};
			parameters[0].Value = model.cldate;
			parameters[1].Value = model.numofday;
			parameters[2].Value = model.ljbp_sec_id;
			parameters[3].Value = model.cdbh;
			parameters[4].Value = model.cdcgqbh;
			parameters[5].Value = model.ljbpwy;
			parameters[6].Value = model.ljbpqcqxj_qx;
			parameters[7].Value = model.ljbpqcqxj_zx;
			parameters[8].Value = model.ljbpjyl;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(HYSoft.Model.bpcddata model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tb_bpcddata set ");
			strSql.Append("numofday=@numofday,");
			strSql.Append("ljbpwy=@ljbpwy,");
			strSql.Append("ljbpqcqxj_qx=@ljbpqcqxj_qx,");
			strSql.Append("ljbpqcqxj_zx=@ljbpqcqxj_zx,");
			strSql.Append("ljbpjyl=@ljbpjyl");
			strSql.Append(" where cdid=@cdid");
			SqlParameter[] parameters = {
					new SqlParameter("@numofday", SqlDbType.Int,4),
					new SqlParameter("@ljbpwy", SqlDbType.Int,4),
					new SqlParameter("@ljbpqcqxj_qx", SqlDbType.Int,4),
					new SqlParameter("@ljbpqcqxj_zx", SqlDbType.Int,4),
					new SqlParameter("@ljbpjyl", SqlDbType.Int,4),
					new SqlParameter("@cdid", SqlDbType.Int,4),
					new SqlParameter("@cldate", SqlDbType.DateTime),
					new SqlParameter("@ljbp_sec_id", SqlDbType.Int,4),
					new SqlParameter("@cdbh", SqlDbType.Int,4),
					new SqlParameter("@cdcgqbh", SqlDbType.Int,4)};
			parameters[0].Value = model.numofday;
			parameters[1].Value = model.ljbpwy;
			parameters[2].Value = model.ljbpqcqxj_qx;
			parameters[3].Value = model.ljbpqcqxj_zx;
			parameters[4].Value = model.ljbpjyl;
			parameters[5].Value = model.cdid;
			parameters[6].Value = model.cldate;
			parameters[7].Value = model.ljbp_sec_id;
			parameters[8].Value = model.cdbh;
			parameters[9].Value = model.cdcgqbh;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int cdid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_bpcddata ");
			strSql.Append(" where cdid=@cdid");
			SqlParameter[] parameters = {
					new SqlParameter("@cdid", SqlDbType.Int,4)
			};
			parameters[0].Value = cdid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(DateTime cldate,int ljbp_sec_id,int cdbh,int cdcgqbh)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_bpcddata ");
			strSql.Append(" where cldate=@cldate and ljbp_sec_id=@ljbp_sec_id and cdbh=@cdbh and cdcgqbh=@cdcgqbh ");
			SqlParameter[] parameters = {
					new SqlParameter("@cldate", SqlDbType.DateTime),
					new SqlParameter("@ljbp_sec_id", SqlDbType.Int,4),
					new SqlParameter("@cdbh", SqlDbType.Int,4),
					new SqlParameter("@cdcgqbh", SqlDbType.Int,4)			};
			parameters[0].Value = cldate;
			parameters[1].Value = ljbp_sec_id;
			parameters[2].Value = cdbh;
			parameters[3].Value = cdcgqbh;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string cdidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_bpcddata ");
			strSql.Append(" where cdid in ("+cdidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HYSoft.Model.bpcddata GetModel(int cdid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 cdid,cldate,numofday,ljbp_sec_id,cdbh,cdcgqbh,ljbpwy,ljbpqcqxj_qx,ljbpqcqxj_zx,ljbpjyl from tb_bpcddata ");
			strSql.Append(" where cdid=@cdid");
			SqlParameter[] parameters = {
					new SqlParameter("@cdid", SqlDbType.Int,4)
			};
			parameters[0].Value = cdid;

			HYSoft.Model.bpcddata model=new HYSoft.Model.bpcddata();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HYSoft.Model.bpcddata DataRowToModel(DataRow row)
		{
			HYSoft.Model.bpcddata model=new HYSoft.Model.bpcddata();
			if (row != null)
			{
				if(row["cdid"]!=null && row["cdid"].ToString()!="")
				{
					model.cdid=int.Parse(row["cdid"].ToString());
				}
				if(row["cldate"]!=null && row["cldate"].ToString()!="")
				{
					model.cldate=DateTime.Parse(row["cldate"].ToString());
				}
				if(row["numofday"]!=null && row["numofday"].ToString()!="")
				{
					model.numofday=int.Parse(row["numofday"].ToString());
				}
				if(row["ljbp_sec_id"]!=null && row["ljbp_sec_id"].ToString()!="")
				{
					model.ljbp_sec_id=int.Parse(row["ljbp_sec_id"].ToString());
				}
				if(row["cdbh"]!=null && row["cdbh"].ToString()!="")
				{
					model.cdbh=int.Parse(row["cdbh"].ToString());
				}
				if(row["cdcgqbh"]!=null && row["cdcgqbh"].ToString()!="")
				{
					model.cdcgqbh=int.Parse(row["cdcgqbh"].ToString());
				}
				if(row["ljbpwy"]!=null && row["ljbpwy"].ToString()!="")
				{
					model.ljbpwy=int.Parse(row["ljbpwy"].ToString());
				}
				if(row["ljbpqcqxj_qx"]!=null && row["ljbpqcqxj_qx"].ToString()!="")
				{
					model.ljbpqcqxj_qx=int.Parse(row["ljbpqcqxj_qx"].ToString());
				}
				if(row["ljbpqcqxj_zx"]!=null && row["ljbpqcqxj_zx"].ToString()!="")
				{
					model.ljbpqcqxj_zx=int.Parse(row["ljbpqcqxj_zx"].ToString());
				}
				if(row["ljbpjyl"]!=null && row["ljbpjyl"].ToString()!="")
				{
					model.ljbpjyl=int.Parse(row["ljbpjyl"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select cdid,cldate,numofday,ljbp_sec_id,cdbh,cdcgqbh,ljbpwy,ljbpqcqxj_qx,ljbpqcqxj_zx,ljbpjyl ");
			strSql.Append(" FROM tb_bpcddata ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" cdid,cldate,numofday,ljbp_sec_id,cdbh,cdcgqbh,ljbpwy,ljbpqcqxj_qx,ljbpqcqxj_zx,ljbpjyl ");
			strSql.Append(" FROM tb_bpcddata ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
        /// <summary>
        /// 整个句子
        /// </summary>
        /// <param name="all"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataSet GetList(string all,string sql) {
            StringBuilder strSql = new StringBuilder();
            if (sql.Trim() != "")
            {
                strSql.Append(sql);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tb_bpcddata ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.cdid desc");
			}
			strSql.Append(")AS Row, T.*  from tb_bpcddata T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}
        /// <summary>
        /// 根据整段的sql语句得到dataset
        /// </summary>
        /// <param name="strsql"></param>
        /// <returns></returns>
        public DataSet Listmany(string strsql)
        {
            return DbHelperSQL.Query(strsql);
        }
		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "tb_bpcddata";
			parameters[1].Value = "cdid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

